package cn.wolfcode.wolf2w.controller;


import cn.wolfcode.wolf2w.annotation.RequiredLogin;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.redis.service.IStrategyStatisVoRedisService;
import cn.wolfcode.wolf2w.redis.service.IUserInfoRedisService;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.util.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("users")
@Api(value = "用户资源",description = "用户资源控制器")
public class UserContorller {

    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private IUserInfoRedisService userInfoRedisService;
    @Autowired
    private IStrategyStatisVoRedisService statisVoRedisService;


    @GetMapping("/get")
    public Object get(String id){
        return userInfoService.get(id);
    }

    @GetMapping("/checkPhone")
    public Object checkPhone(String phone){

        boolean ret = userInfoService.checkPhone(phone);
        return !ret;
    }

    @GetMapping("/sendVerifyCode")
    public Object sendVerifyCode(String phone){
        userInfoService.sendVerifyCode(phone);
        return JsonResult.success();
    }

    @ApiOperation(value = "注册功能",notes = "其实就是新增用户")

    @ApiImplicitParams({
            @ApiImplicitParam(value = "手机号",name = "phone",dataType = "String",required = true),
            @ApiImplicitParam(value = "昵称",name = "nickname",dataType = "String",required = true),
            @ApiImplicitParam(value = "密码",name = "password",dataType = "String",required = true),
            @ApiImplicitParam(value = "确认密码",name = "rpassword",dataType = "String",required = true),
            @ApiImplicitParam(value = "验证码",name = "verifyCode",dataType = "String",required = true)
    })

    @PostMapping("/regist")
    public Object regist(String nickname,String password,String rpassword,String phone,String verifyCode){
        userInfoService.regist(nickname,password,rpassword,phone,verifyCode);
        return JsonResult.success();
    }

    @PostMapping("/login")
    public Object login(String username,String password){
        //用户登录
        UserInfo userInfo = userInfoService.login(username, password);

        //创建登录令牌token
        String token = userInfoRedisService.setToken(userInfo);

        Map<String,Object> map = new HashMap<>();
        map.put("token", token);
        map.put("user", userInfo);
        return JsonResult.success(map);
    }

    @RequiredLogin
    @GetMapping("/currentUser")
    public Object currentUser(HttpServletRequest request){
        //从请求头获取token
        String token = request.getHeader("token");
        UserInfo userInfo = userInfoRedisService.getUserInfoByToken(token);

        return JsonResult.success(userInfo);
    }

    @GetMapping("/strategies/favor")
    public Object strategyFavor(String uid){
        return JsonResult.success(statisVoRedisService.getUserStrategyFavor(uid));
    }
}
